package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "套餐接口")
public class SetmealController {
	
	@Autowired
	private SetmealService setmealService;
	
	/**
	 * 分页查询
	 */
	@GetMapping("/page")
	@ApiOperation("分页查询")
	public Result<PageResult> getSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
		log.info("分页查询：{}", setmealPageQueryDTO);
		return Result.success(setmealService.getSetmeal(setmealPageQueryDTO));
	}
	
	/**
	 * 新增菜品
	 */
	@PostMapping
	@ApiOperation("新增菜品")
	public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
		log.info("新增菜品：{}", setmealDTO);
		setmealService.addSetmeal(setmealDTO);
		return Result.success();
	}
	
	/**
	 * 根据id查询数据
	 */
	@GetMapping("/{id}")
	@ApiOperation("根据id查询")
	@Cacheable(cacheNames = "SetmealCache", key = "#id")
	public Result<SetmealVO> getSetmealId(@PathVariable Long id) {
		log.info("根据id查询：{}", id);
		return Result.success(setmealService.getSetmealId(id));
	}
	
	/**
	 * 起售/停售套餐
	 */
	@PostMapping("/status/{status}")
	@ApiOperation("起售/停售套餐")
	@CacheEvict(cacheNames = "setmealCache", allEntries = true)
	public Result putSetmealStatus(Long id, @PathVariable Integer status) {
		setmealService.putSetmealStatus(id, status);
		return Result.success();
	}
	
	/**
	 * 删除套餐
	 */
	@DeleteMapping
	@ApiOperation("删除套餐")
	@CacheEvict(cacheNames = "setmealCache", allEntries = true)
	public Result deleteSetmeal(@RequestParam List<Long> ids) {
		log.info("删除套餐：{}", ids);
		setmealService.deleteSetmeal(ids);
		return Result.success();
	}
	
	/**
	 * 修改套餐
	 */
	@PutMapping
	@ApiOperation("修改套餐")
	@CacheEvict(cacheNames = "setmealCache", allEntries = true)
	public Result putSetmeal(@RequestBody SetmealDTO setmealDTO) {
		setmealService.putSetmeal(setmealDTO);
		return Result.success();
	}
	
}
